package com.bygf.curve.demo;

import javax.swing.DefaultListModel;
import javax.swing.JPanel;

/**
 *
 * @author sunyupei
 */
public class Demo extends javax.swing.JFrame {

    JPanel cp;
    MemoryDemo memoryDemo = new MemoryDemo();

    /**
     * Creates new form Demo
     */
    public Demo() {
        initComponents();
        initDemoList();
        memoryParentPanel.add(memoryDemo);
        memoryDemo.setBounds(1, 1, memoryParentPanel.getWidth() - 2, memoryParentPanel.getHeight() - 2);
    }

    private void initDemoList() {
        DefaultListModel dlm = new DefaultListModel();
        dlm.addElement("基本演示");
        dlm.addElement("双坐标演示");
        dlm.addElement("警戒线演示");
        dlm.addElement("标尺及缩放演示");
        dlm.addElement("时间曲线演示");
        dlm.addElement("区域曲线演示");
        dlm.addElement("渐变曲线演示");
        demoList.setModel(dlm);
        demoList.setSelectedIndex(0);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        demoList = new javax.swing.JList();
        parentPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        memoryParentPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CurveDemo");
        setMinimumSize(new java.awt.Dimension(850, 500));

        jScrollPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        demoList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        demoList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                demoListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(demoList);

        parentPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        parentPanel.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                parentPanelComponentResized(evt);
            }
        });

        javax.swing.GroupLayout parentPanelLayout = new javax.swing.GroupLayout(parentPanel);
        parentPanel.setLayout(parentPanelLayout);
        parentPanelLayout.setHorizontalGroup(
            parentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 645, Short.MAX_VALUE)
        );
        parentPanelLayout.setVerticalGroup(
            parentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(parentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 451, Short.MAX_VALUE)
                    .addComponent(parentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Demos", jPanel1);

        memoryParentPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        memoryParentPanel.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                memoryParentPanelComponentResized(evt);
            }
        });

        javax.swing.GroupLayout memoryParentPanelLayout = new javax.swing.GroupLayout(memoryParentPanel);
        memoryParentPanel.setLayout(memoryParentPanelLayout);
        memoryParentPanelLayout.setHorizontalGroup(
            memoryParentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 843, Short.MAX_VALUE)
        );
        memoryParentPanelLayout.setVerticalGroup(
            memoryParentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 469, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(memoryParentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(memoryParentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Memory", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void demoListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_demoListValueChanged
        if (demoList.getSelectedValue() == null) {
            return;
        }
        if (evt.getValueIsAdjusting()) {
            return;
        }
        String demo = demoList.getSelectedValue().toString();
        parentPanel.removeAll();
        switch (demo) {
            case "基本演示":
                cp = new XYCurveDemo1();
                break;
            case "双坐标演示":
                cp = new XYCurveDemo2();
                break;
            case "警戒线演示":
                cp = new XYCurveDemo3();
                break;
            case "标尺及缩放演示":
                cp = new XYCurveDemo4();
                break;
            case "时间曲线演示":
                cp = new TimeCurveDemo1();
                break;
            case "区域曲线演示":
                cp = new AreaCurveDemo1();
                break;
            case "渐变曲线演示":
                cp = new LinearGradientCurveDemo1();
                break;
        }
        cp.setBounds(1, 1, parentPanel.getWidth(), parentPanel.getHeight());
        parentPanel.add(cp);
        parentPanel.updateUI();
        System.gc();
    }//GEN-LAST:event_demoListValueChanged

    private void parentPanelComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_parentPanelComponentResized
        if (cp != null) {
            cp.setBounds(1, 1, parentPanel.getWidth(), parentPanel.getHeight());
        }
    }//GEN-LAST:event_parentPanelComponentResized

    private void memoryParentPanelComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_memoryParentPanelComponentResized
        memoryDemo.setBounds(1, 1, memoryParentPanel.getWidth() - 2, memoryParentPanel.getHeight() - 2);
    }//GEN-LAST:event_memoryParentPanelComponentResized

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Demo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Demo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Demo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Demo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Demo().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList demoList;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel memoryParentPanel;
    private javax.swing.JPanel parentPanel;
    // End of variables declaration//GEN-END:variables
}
